package com.houfukude.Activity;

import com.houfukude.Utils.ActionBar_Utils;
import com.houfukude.xtestmgr.R;

import Utils.File_Utils;
import Utils.Note_Utils;
import Utils.V;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * 
 * @author houfukude
 * 
 */
public class NewNoteActivity extends ActionBarActivity {

	private static final String DEBUG_TAG = "NewNoteActivity";

	private ImageView imageView;

	private int width = 300;
	private int height = 300;

	protected String param = null;
	protected String path = null;

	private OnClickListener iv_Click = new OnClickListener() {

		@Override
		public void onClick(View arg0) {

			Log.i(DEBUG_TAG, "preview path : " + path);
			if (path != null) {
				Toast.makeText(getApplicationContext(), "正在打开...",
						Toast.LENGTH_SHORT).show();
				Intent preview = Note_Utils.getPreviewIntent(path, param);
				startActivityForResult(preview, 0);
			}

		}
	};
	/**
	 * 对图片进行涂鸦
	 */
	private OnLongClickListener iv_longClick = new OnLongClickListener() {

		@Override
		public boolean onLongClick(View view) {
			Log.i(DEBUG_TAG, "draw path : " + path);
			Intent imageDraw = Note_Utils.getImageDrawIntent(path);
			if (imageDraw != null) {
				startActivityForResult(imageDraw, V.id.ADD_IMAGE);
			} else {
				Toast.makeText(getApplicationContext(), "当前非图片，不能修改！",
						Toast.LENGTH_SHORT).show();
			}
			return false;
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_record);
		imageView = (ImageView) findViewById(R.id.imageView1);
		imageView.setOnClickListener(iv_Click);
		imageView.setOnLongClickListener(iv_longClick);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.new_note, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Log.d(DEBUG_TAG, "ItemSelected item.getItemId():" + item.getItemId());
		switch (item.getItemId()) {
		case R.id.action_note_save:
			ActionBar_Utils.NoteSaveItemSelected(item, this);
			break;
		case R.id.action_add_image:
			ActionBar_Utils.AddImageItemSelected(item, this);
			break;

		case R.id.action_add_audio:
			ActionBar_Utils.AddAudioItemSelected(item, this);
			break;

		case R.id.action_add_video:
			ActionBar_Utils.AddVideoItemSelected(item, this);
			break;
		case R.id.action_settings:
			Toast.makeText(this, "settings for " + getTitle(),
					Toast.LENGTH_SHORT).show();
			break;
		}
		return super.onContextItemSelected(item);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Bitmap bmp = null;

		// super.onActivityResult(requestCode, resultCode, data);
		Log.i(DEBUG_TAG, "requestCode : " + requestCode + " resultCode :"
				+ resultCode);
		if (resultCode == RESULT_CANCELED) {
			Log.d(DEBUG_TAG, "action canceled !");
			return;
		}
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case V.id.ADD_IMAGE:
				param = "image/*";
				if (data != null) {
					path = Note_Utils.getPath(getApplicationContext(),
							data.getData());
					Log.i(DEBUG_TAG, " get photo at ：" + path);
					bmp = Note_Utils.getImageThumbnail(getApplicationContext(),
							path);
					if (bmp == null) {
						bmp = Note_Utils.getImageZoom(path, width, height);
						Log.i(DEBUG_TAG, "create image thumbs for ：" + path);
					}
					imageView.setImageBitmap(bmp);
				} else {
					String name = ActionBar_Utils.Image_Name;
					path = V.Image_Directory + name;
					/**
					 * 重新设置为空
					 */
					ActionBar_Utils.Image_Name = null;

					Log.i(DEBUG_TAG, " new photo at ：" + path);
					bmp = Note_Utils.getImageZoom(path, width, height);
					imageView.setImageBitmap(bmp);
				}
				break;
			case V.id.ADD_AUDIO:
				param = "audio/*";
				/**
				 * 通过 Uri uri = Uri.fromFile(new File(path)); 可以返回系统一致的uri
				 */
				path = Note_Utils.getPath(getApplicationContext(),
						data.getData());
				Log.i(DEBUG_TAG, "action_add_audio return path：" + path);
				imageView.setImageDrawable(getResources().getDrawable(
						R.drawable.audio));
				break;
			case V.id.ADD_VIDEO:
				param = "video/*";
				path = Note_Utils.getPath(getApplicationContext(),
						data.getData());
				Log.i(DEBUG_TAG, "action_add_video  return path：" + path);
				Log.i(DEBUG_TAG,
						"action_add_video  data.getFlags :" + data.getFlags());
				/**
				 * 
				 * 当初我用 data.getAction() 来判断，一个为空另一个不为空，感觉很坑爹。<br>
				 * 现在我用 data.getFlags()来判断，一个为1一个为0感觉要好点。>,,< <br>
				 * 但是！！！尼玛为什么我用root explorer的返回还是0啊要不要这么坑爹啊！<br>
				 * 好吧只能在 File_Utils.saveVideoFile里面再做判断了，
				 * 终于知道为什么里面那么多if带return了么!!!!!
				 * 
				 * @author houfukude 这只是吐槽！<br>
				 */
				if (data.getFlags() == 0) {
					path = File_Utils.saveVideoFile(path);
					Log.i(DEBUG_TAG, "video move to ：" + path);
				}
				bmp = Note_Utils.getVideoThumbnail(getApplicationContext(),
						path);
				if (bmp == null) {
					bmp = Note_Utils.getVideoThumbnail(path);
					Log.i(DEBUG_TAG, "create video thumbs for ：" + path);
				}
				imageView.setImageBitmap(bmp);
				break;
			default:
				break;
			}
		}

		if (data == null) {
			Log.d(DEBUG_TAG, "data is null ");
			return;
		}

	}
}
